// pages/logins/login/index.js
const app = getApp()
import md5 from '../../../utils/md5.js'
import tool from '../../../utils/tools.js'
import api from '../../../api/main.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userType: '',
    title: '',
    userPhone: '',
    phoneCode: '',
    codeButtonString: '获取',
    isSendMeesage: true,
    codeMessageTime: 60
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.type) {
      this.setData({
        userType: options.type,
        title: options.type == '1' ? '法官登录' : '群众登录'
      })
    }
  },
  /**
   * 返回
   */
  onBack() {
    wx.navigateBack()
  },
  /**
   * 手机号码输入改变
   */
  changUserPhone: function(e) {
    this.setData({
      userPhone: e.detail.value
    })
  },
  /**
   * 验证码输入改变
   */
  changPhoneCode: function(e) {
    this.setData({
      phoneCode: e.detail.value
    })
  },
  /**
   * 获取验证码
   */
  getMeesageCode() {
    const that = this;
    //输入验证
    if (that.checkInput([that.data.userPhone], ['请输入手机号码'])) {
      return
    }
    if (!that.data.isSendMeesage) {
      return;
    }
    api.login.getPhoneCode(that.data.userPhone, that.data.userType).then(res => {
      if (res.code != app.globalData.successCode) {
        wx.showToast({
          title: code.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
    var currentTime = that.data.codeMessageTime
    var interval = setInterval(function() {
      currentTime--;
      that.setData({
        codeButtonString: currentTime + '秒',
        isSendMeesage: false
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          codeButtonString: '重新发送',
          codeMessageTime: 60,
          isSendMeesage: true
        })
      }
    }, 1000)
  },
  /**
   * 获取短信验证码
   * @param 
   */

  /**
   * 用户登录
   */
  onLogin() {
    const that = this;
    // 输入验证
    if (that.checkInput([that.data.userPhone, that.data.phoneCode], ['请输入手机号码', '请输入验证码'])) {
      return
    }
    wx.showLoading({
      title: '正在登录...',
      mask: true
    })
    api.login.userLogin(that.data.userType, that.data.userPhone, that.data.phoneCode).then(res => {
      wx.hideLoading()
      if (res.code == app.globalData.successCode && res.success) {
        app.globalData.token = res.data.token
        app.globalData.userData = res.data
        if(that.userType == '1'){
          app.globalData.courtId = res.data.courtId
          app.globalData.courtName = res.data.court.name
        }
        // 保存token
        wx.setStorage({
          key: app.globalData.tokenKey,
          data: res.data.token
        })
        // 保存用户类型
        wx.setStorage({
          key: app.globalData.userTypeKey,
          data: that.data.userType,
        })
        // 保存用户数据
        wx.setStorage({
          key: app.globalData.userDataKey,
          data: res.data,
        })
        if (that.data.userType == '1') {
          wx.reLaunch({
            url: '../../index/judge/index',
          })
        } else {
          wx.reLaunch({
            url: '../../index/people/index',
          })
        }
      } else {
        wx.showToast({
          icon: 'none',
          title: res.msg == undefined ? "请求错误" : res.msg,
          duration: 2000
        })
      }
    })
  },
  /**
   * 输入验证码
   * @param array 需要验证的输入框数组 0 手机号码 1 验证码
   * */
  checkInput(array, messages) {
    var result = false
    for (var i = 0; i < array.length; i++) {
      const value = array[i];
      const message = messages[i];
      //验证是否输入
      if (value == '') {
        wx.showToast({
          title: message,
          icon: 'none'
        })
        result = true
        break
      }
      if (i == 0 && !tool.isPone(value)) {
        //验证手机是否合法
        wx.showToast({
          title: '手机号码不合法',
          icon: 'none'
        })
        result = true
        break
      }
    }
    return result
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})